create or replace package body m4o_logging_advice as
  -- ===========================================================================

  C_MODULE constant m4o_module := new m4o_module('m4o_logging_advice');

  -- ===========================================================================
  procedure apply_before_body(
    i_package in out nocopy m4o_package_generator.package_t)
  is
    v_pc m4o_context := C_MODULE.proc('apply_before_body');
    v_part m4o_package_generator.part_t;
  begin
    v_part.name       := 'C_MODULE';
    v_part.is_public  := 'N';
    v_part.type_id    := m4o_package_generator.C_DECLARATION_TYPEID;
    v_part.sourcecode :=
      '  C_MODULE constant m4o_module := new m4o_module('''||
      i_package.name||
      ''');';
    i_package.parts(i_package.parts.first-1) := v_part;
  end;

  -- ===========================================================================
  procedure apply_before_body_part(
    i_part in out nocopy m4o_package_generator.part_t)
  is
    v_pc m4o_context := C_MODULE.proc('apply_before_body');
    v_pc_stmt varchar2(4000);
    v_args    varchar2(4000);
    -- -------------------------------------------------------------------------
    function printable_form(i_arg in m4o_package_generator.argument_t)
    return varchar2
    is
      v_arg_name varchar2(4000) := lower(i_arg.name);
    begin
      return
        case
          when i_arg.data_type in ('FLOAT','NUMBER','BINARY_INTEGER',
                                   'CHAR','VARCHAR2') then
            v_arg_name
          when i_arg.data_type = 'DATE' then
            'to_char('||v_arg_name||',''yyyy-mm-dd'')'
          when i_arg.data_type = 'PL/SQL BOOLEAN' then
            'case '||
              'when '|| v_arg_name|| ' then ''<TRUE>'' '||
              'when '||v_arg_name||' is null then null '||
              'else ''<FALSE>'' '||
            'end'
          else
            '''<'||
            lower(i_arg.data_type)||
            ' '||
            v_arg_name||
            '>'''
        end;
    end;
    -- -------------------------------------------------------------------------
  begin
    if i_part.parsed_arguments.count > 0 then
      v_pc_stmt :=
        'v_pc$args m4o_v2tab := case when m4o_dbg.p_level > 1 then'||
        chr(10)||
        '                             m4o_v2tab(#args#)'||chr(10)||
        '                           else null'||chr(10)||
        '                           end;';
      for i in 1 .. i_part.parsed_arguments.count loop
        v_args :=
          v_args||
          case
          when i > 1 then ','
          end||
          printable_form(i_part.parsed_arguments(i));
      end loop;
      v_pc_stmt := replace(v_pc_stmt,
                           '#args#',
                           v_args);
    else
      v_pc_stmt :=
        'v_pc$args m4o_v2tab := null;';
    end if;

    v_pc_stmt :=
      v_pc_stmt||
      chr(10)||
      '    v_pc m4o_context := C_MODULE.proc('''||
      case
      when i_part.type_id=m4o_package_generator.C_INIT_TYPEID then
           '<init>'
      else i_part.name
      end||
      ''',v_pc$args);';

    i_part.local_declarations := 
      rtrim(v_pc_stmt||
            chr(10)||
            '    '||
            i_part.local_declarations,
            m4o_v2.WS);
  end;

end;
/
